<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="车辆类型id" prop="truckTypeId">
      <el-input v-model="dataForm.truckTypeId" placeholder="车辆类型id"></el-input>
    </el-form-item>
    <el-form-item label="所属车队id" prop="fleetId">
      <el-input v-model="dataForm.fleetId" placeholder="所属车队id"></el-input>
    </el-form-item>
    <el-form-item label="品牌" prop="brand">
      <el-input v-model="dataForm.brand" placeholder="品牌"></el-input>
    </el-form-item>
    <el-form-item label="车牌号码" prop="licensePlate">
      <el-input v-model="dataForm.licensePlate" placeholder="车牌号码"></el-input>
    </el-form-item>
    <el-form-item label="GPS设备id" prop="deviceGpsId">
      <el-input v-model="dataForm.deviceGpsId" placeholder="GPS设备id"></el-input>
    </el-form-item>
    <el-form-item label="准载重量" prop="allowableLoad">
      <el-input v-model="dataForm.allowableLoad" placeholder="准载重量"></el-input>
    </el-form-item>
    <el-form-item label="准载体积" prop="allowableVolume">
      <el-input v-model="dataForm.allowableVolume" placeholder="准载体积"></el-input>
    </el-form-item>
    <el-form-item label="车辆行驶证信息id" prop="truckLicenseId">
      <el-input v-model="dataForm.truckLicenseId" placeholder="车辆行驶证信息id"></el-input>
    </el-form-item>
    <el-form-item label="状态 0：禁用 1：正常" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态 0：禁用 1：正常"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          truckTypeId: '',
          fleetId: '',
          brand: '',
          licensePlate: '',
          deviceGpsId: '',
          allowableLoad: '',
          allowableVolume: '',
          truckLicenseId: '',
          status: ''
        },
        dataRule: {
          truckTypeId: [
            { required: true, message: '车辆类型id不能为空', trigger: 'blur' }
          ],
          fleetId: [
            { required: true, message: '所属车队id不能为空', trigger: 'blur' }
          ],
          brand: [
            { required: true, message: '品牌不能为空', trigger: 'blur' }
          ],
          licensePlate: [
            { required: true, message: '车牌号码不能为空', trigger: 'blur' }
          ],
          deviceGpsId: [
            { required: true, message: 'GPS设备id不能为空', trigger: 'blur' }
          ],
          allowableLoad: [
            { required: true, message: '准载重量不能为空', trigger: 'blur' }
          ],
          allowableVolume: [
            { required: true, message: '准载体积不能为空', trigger: 'blur' }
          ],
          truckLicenseId: [
            { required: true, message: '车辆行驶证信息id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态 0：禁用 1：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/aggregation/truck/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.truckTypeId = data.truck.truckTypeId
                this.dataForm.fleetId = data.truck.fleetId
                this.dataForm.brand = data.truck.brand
                this.dataForm.licensePlate = data.truck.licensePlate
                this.dataForm.deviceGpsId = data.truck.deviceGpsId
                this.dataForm.allowableLoad = data.truck.allowableLoad
                this.dataForm.allowableVolume = data.truck.allowableVolume
                this.dataForm.truckLicenseId = data.truck.truckLicenseId
                this.dataForm.status = data.truck.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/aggregation/truck/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'truckTypeId': this.dataForm.truckTypeId,
                'fleetId': this.dataForm.fleetId,
                'brand': this.dataForm.brand,
                'licensePlate': this.dataForm.licensePlate,
                'deviceGpsId': this.dataForm.deviceGpsId,
                'allowableLoad': this.dataForm.allowableLoad,
                'allowableVolume': this.dataForm.allowableVolume,
                'truckLicenseId': this.dataForm.truckLicenseId,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
